from graphics import *
import time
import random

# Ball Bouncing

width = 100
height = 100

def main():
    win = GraphWin("My Bouncing Ball", width, height)

    x = random.randint(0, width-1)
    y = random.randint(0, height-1)

    dx = 1 # To the right
    dy = 1 # Downward

    ball = Circle(Point(x,y), 5)
    ball.setFill('blue')
    ball.draw(win)

    while True:
        ball.move(dx, dy)
        time.sleep(0.05)

        ball_spot = ball.getCenter()
        if ball_spot.getX() < 0: # X needs to be positive!
            dx = 1
        if ball_spot.getX() >= width: # X needs to be negative!
            dx = -1
        if ball_spot.getY() < 0: # Y needs to be positive!
            dy = 1
        if ball_spot.getY() >= height: # Y needs to be negative!
            dy = -1

main()
